from PyQt5.QtGui import QFont, QPalette, QColor
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *


class MyQDialog(QDialog):
    def __init__(self):
        #  QDialog.__init__(self, parent)
        super(MyQDialog, self).__init__()
        self.resize(200, 60)
        self.setWindowTitle(u"QLineEdit加正则")

        regExp = QtCore.QRegExp('^((2[0-4]\d|25[0-5]|[1-9]?\d|1\d{2})\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?):\d{1,5}$')

        self.IP_edit = QLineEdit("", self)

        palette = self.IP_edit.palette()
        palette.setColor(QPalette.Base, QColor("black"))  # 设置背景颜色为红色
        self.IP_edit.setAutoFillBackground(True)

        palette.setColor(QPalette.Text, QColor("white"))
        self.IP_edit.setPalette(palette)

        self.IP_edit.setFont(QFont('微软雅黑', 20))
        self.IP_edit.setValidator(QtGui.QRegExpValidator(regExp, self))
        self.IP_edit.setGeometry(QtCore.QRect(10, 20, 180, 36))


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    ui = MyQDialog()
    ui.show()
    app.exec_()
